package com.jgc.locks;


import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by Administrator on 2022/3/23.
 */
public class Ticket {
    private int num = 50;
    private Lock lock = new ReentrantLock(true);

    public void sale(){
        lock.lock();
        try {
            if(num>0){
                System.out.println(Thread.currentThread().getName() + "\t卖出第：" + (num--) + "\t还剩下" + num);
            }
        }finally {
            lock.unlock();
        }

    }
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        new Thread(()-> { for(int i=1;i<55;i++) ticket.sale();},"a").start();
        new Thread(()-> { for(int i=1;i<55;i++) ticket.sale();},"b").start();
        new Thread(()-> { for(int i=1;i<55;i++) ticket.sale();},"c").start();
        new Thread(()-> { for(int i=1;i<55;i++) ticket.sale();},"d").start();
        new Thread(()-> { for(int i=1;i<55;i++) ticket.sale();},"e").start();
    }
}
